package com.pasabahceambalaj.session;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;
import org.richfaces.model.DataProvider;
import org.richfaces.model.ExtendedTableDataModel;
import org.richfaces.model.selection.Selection;
import org.richfaces.model.selection.SimpleSelection;

import com.pasabahceambalaj.entity.Firma;
import com.pasabahceambalaj.entity.Harekettur;
import com.pasabahceambalaj.entity.Hareketturgrup;
import com.pasabahceambalaj.entity.Isemir;
import com.pasabahceambalaj.entity.Isemirhareket;
import com.pasabahceambalaj.entity.Isemirmalzeme;
import com.pasabahceambalaj.entity.Isemirurun;
import com.pasabahceambalaj.entity.Kalip;
import com.pasabahceambalaj.entity.Olcubirim;
import com.pasabahceambalaj.entity.Sevk;

@Name("isemirhareketHome")
public class IsemirhareketHome extends EntityHome<Isemirhareket> {

	@In(create = true)
	HareketturHome hareketturHome;
		
	@In(value="isemirHome", create = true)
	IsemirHome isemirHome4VirmanId;
	
	@In(value="isemirHome", create = true)
	IsemirHome isemirHome4IsemirId;
	
	@In(create = true)
	IsemirmalzemeHome isemirmalzemeHome;
	
	@In(create = true)
	IsemirurunHome isemirurunHome;
	
	@In(create = true)
	SevkHome sevkHome;
	
//	@In(create = true)
//	IsemirurunList isemirurunList;
//	
//	@In(create = true)
//	IsemirmalzemeList isemirmalzemeList;

	@In(create = true)
	IsemirList isemirList;

	
	private Selection selection = new SimpleSelection();
	private String sortMode="single";
    private String selectionMode="single";
    private Object tableState;
    private ExtendedTableDataModel<Isemirurun> dataModel;
    private List<Isemirurun> isemiruruns = new ArrayList<Isemirurun>();
    private List<Isemirurun> selectedIsemiruruns = new ArrayList<Isemirurun>();
    
    private Selection selection2 = new SimpleSelection();
	private String sortMode2="single";
    private String selectionMode2="single";
    private Object tableState2;
    private ExtendedTableDataModel<Isemirmalzeme> dataModel2;
    private List<Isemirmalzeme> isemirMalzemes = new ArrayList<Isemirmalzeme>();
    private List<Isemirmalzeme> selectedIsemirMalzemes = new ArrayList<Isemirmalzeme>();
    
    private Selection selection3 = new SimpleSelection();
	private String sortMode3="single";
    private String selectionMode3="single";
    private Object tableState3;
    private ExtendedTableDataModel<Isemir> dataModel3;
    private List<Isemir> isemirs = new ArrayList<Isemir>();
    private List<Isemir> selectedIsemirs = new ArrayList<Isemir>();
    

	private String hareketCinsi = "urun";
	
	public void setIsemirhareketId(Long id) {
		setId(id);
	}

	public Long getIsemirhareketId() {
		return (Long) getId();
	}

	@Override
	protected Isemirhareket createInstance() {
		Isemirhareket isemirhareket = new Isemirhareket();
		return isemirhareket;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		
		Harekettur harekettur = hareketturHome.getDefinedInstance();
		if (harekettur != null) {
			getInstance().setHarekettur(harekettur);
			getInstance().setHareketturgrup(harekettur.getHareketturgrup());
		}
//		Hareketturgrup hareketturgrup = hareketturgrupHome.getDefinedInstance();
//		if (hareketturgrup != null) {
//			getInstance().setHareketturgrup(hareketturgrup);
//		}
		
		if(selectedIsemirs.size()>0){
			Isemir isemirByIsemirid = selectedIsemirs.get(0);
			if (isemirByIsemirid != null) {
				getInstance().setIsemirByIsemirid(isemirByIsemirid);
			}			
		}
		
		Isemir isemirByIsemirid = isemirHome4IsemirId.getDefinedInstance();
		if (isemirByIsemirid != null) {
			getInstance().setIsemirByIsemirid(isemirByIsemirid);
		}
		
		Isemir isemirByVirmanisemirid = isemirHome4VirmanId.getDefinedInstance();
		if (isemirByVirmanisemirid != null) {
			getInstance().setIsemirByVirmanisemirid(isemirByVirmanisemirid);
		}
		Isemirmalzeme isemirmalzeme = isemirmalzemeHome.getDefinedInstance();
		if (isemirmalzeme != null) {
			getInstance().setIsemirmalzeme(isemirmalzeme);
		}
		Isemirurun isemirurun = isemirurunHome.getDefinedInstance();
		if (isemirurun != null) {
			getInstance().setIsemirurun(isemirurun);
		}
		Sevk sevk = sevkHome.getDefinedInstance();
		if (sevk != null) {
			getInstance().setSevk(sevk);
		}
		
		isemirs = isemirList.getResultList();
		
		//isemiruruns = isemirurunList.getResultList();
		//isemirMalzemes = isemirmalzemeList.getResultList();			
		urunVeMalzemeListesiDoldur();
		
		
//		for (Isemirurun ieurun : isemiruruns) {
//			ieurun.getUrun();
//			System.out.println(ieurun.getUrun().getAdi());
//		}
		
	}
	
	public boolean isWired() {
		/*if (getInstance().getHarekettur() == null)
			return false;
		if (getInstance().getHareketturgrup() == null)
			return false;*/
		if (getInstance().getIsemirByIsemirid() == null)
			return false;
		if (getInstance().getSevk() == null)
			return false;
		return true;
	}
	
	private void urunVeMalzemeListesiDoldur(){
		// acilacak urun ve malzeme listesi secilen isemirine ait urun ve
		// malzemeler olmasi icin yapildi.
		if (getInstance().getIsemirByIsemirid() != null) {
			isemiruruns = new ArrayList<Isemirurun>(getInstance().getIsemirByIsemirid().getIsemiruruns());
			isemirMalzemes = new ArrayList<Isemirmalzeme>(getInstance().getIsemirByIsemirid().getIsemirmalzemes());
			
			Isemirurun isemirurun = new Isemirurun();
			isemirurun.setIsemir(getInstance().getIsemirByIsemirid());
			Long planlananAdet = getInstance().getIsemirByIsemirid().getPlanlananadet();
			isemirurun.setToplammiktar(planlananAdet.intValue());
			isemirurun.setUrun(getInstance().getIsemirByIsemirid().getUrun());
			//isemirurun.setMiktar();
			isemirurun.setId(-1l);
			isemirurun.setVersion(0);
			isemiruruns.add(0,isemirurun);
			
		}else{
			isemiruruns = new ArrayList<Isemirurun>();
			isemirMalzemes = new ArrayList<Isemirmalzeme>();
		}
		selectedIsemiruruns.clear();
		selectedIsemirMalzemes.clear();
		
		dataModel = null;
		dataModel2 = null;

		for (Isemirurun ieurun : isemiruruns) {
			ieurun.getUrun();
			System.out.println("isemir urun : "+ieurun.getUrun().getAdi());
		}
		
		for (Isemirmalzeme iemalzeme : isemirMalzemes) {
			iemalzeme.getMalzeme();
			//System.out.println("isemir malzeme : "+iemalzeme.getMalzeme().getAd());
		}			
	}
	
	public Isemirhareket getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public void setHareketCinsi(String hareketCinsi) {
		this.hareketCinsi = hareketCinsi;
	}

	public String getHareketCinsi() {
		return hareketCinsi;
	}

	public void takeSelection() {
		//selectedUruns.clear();
		Iterator<Object> iterator = getSelection().getKeys();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			//selectedUruns.add(getUrunsDataModel().getObjectByKey(key));
			if(this.getInstance()!=null){
				this.getInstance().setIsemirurun(getUrunsDataModel().getObjectByKey(key));
				// bir urun secilince secili olan malzemeyi temizliyorum
				// cunku ayni anda hem malzeme hemde urun cikisi ici sayi verilemez
				this.clearSelectedMalzeme();
			}
		}
	}

	public void takeSelection2() {
		//selectedUruns.clear();
		Iterator<Object> iterator = getSelection2().getKeys();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			//selectedUruns.add(getUrunsDataModel().getObjectByKey(key));
			if(this.getInstance()!=null){
				this.getInstance().setIsemirmalzeme(getMalzemesDataModel().getObjectByKey(key));
				// bir urun secilince secili olan malzemeyi temizliyorum
				// cunku ayni anda hem malzeme hemde urun cikisi ici sayi verilemez
				this.clearSelectedUrun();
			}
		}
	}

	public void takeSelection3() {
		//selectedUruns.clear();
		Iterator<Object> iterator = getSelection3().getKeys();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			//selectedUruns.add(getUrunsDataModel().getObjectByKey(key));
			if(this.getInstance()!=null){
				this.getInstance().setIsemirByIsemirid(getIsemirsDataModel().getObjectByKey(key));
			}
		}
		
		urunVeMalzemeListesiDoldur();
		
		System.out.println("is emir idleri :"+this.getInstance().getIsemirByIsemirid().getIsemirno());
	}
	
	public void setSelection(Selection selection) {
		this.selection = selection;
	}

	public Selection getSelection() {
		return selection;
	}

	public void setSelection2(Selection selection) {
		this.selection2 = selection;
	}

	public Selection getSelection2() {
		return selection2;
	}
	
	public void setSelection3(Selection selection) {
		this.selection3 = selection;
	}

	public Selection getSelection3() {
		return selection3;
	}

	public ExtendedTableDataModel<Isemirurun> getUrunsDataModel() {
        if (dataModel == null) {
            dataModel = new ExtendedTableDataModel<Isemirurun>(new DataProvider<Isemirurun>(){

                public Isemirurun getItemByKey(Object key) {
                    for(Isemirurun c : isemiruruns){
                        if (key.equals(getKey(c))){
                            return c;
                        }
                    }
                    return null;
                }

                public List<Isemirurun> getItemsByRange(int firstRow, int endRow) {
                    return isemiruruns.subList(firstRow, endRow);
                }

                public Object getKey(Isemirurun item) {
                    return item.getId();
                }

                public int getRowCount() {
                    return isemiruruns.size();
                }
                
            });
        }
        return dataModel;
    }
	
	public ExtendedTableDataModel<Isemirmalzeme> getMalzemesDataModel() {
        if (dataModel2 == null) {
            dataModel2 = new ExtendedTableDataModel<Isemirmalzeme>(new DataProvider<Isemirmalzeme>(){

                public Isemirmalzeme getItemByKey(Object key) {
                    for(Isemirmalzeme c : isemirMalzemes){
                        if (key.equals(getKey(c))){
                            return c;
                        }
                    }
                    return null;
                }

                public List<Isemirmalzeme> getItemsByRange(int firstRow, int endRow) {
                    return isemirMalzemes.subList(firstRow, endRow);
                }

                public Object getKey(Isemirmalzeme item) {
                    return item.getId();
                }

                public int getRowCount() {
                    return isemirMalzemes.size();
                }                
            });
        }
        return dataModel2;
    }
	
	public ExtendedTableDataModel<Isemir> getIsemirsDataModel() {
        if (dataModel3 == null) {
            dataModel3 = new ExtendedTableDataModel<Isemir>(new DataProvider<Isemir>(){

                public Isemir getItemByKey(Object key) {
                    for(Isemir c : isemirs){
                        if (key.equals(getKey(c))){
                            return c;
                        }
                    }
                    return null;
                }

                public List<Isemir> getItemsByRange(int firstRow, int endRow) {
                    return isemirs.subList(firstRow, endRow);
                }

                public Object getKey(Isemir item) {
                    return item.getId();
                }

                public int getRowCount() {
                    return isemirs.size();
                }                
            });
        }
        return dataModel3;
    }


	public void clearSelectedUrun(){
		if(this.getInstance()!=null){
			this.getInstance().setIsemirurun(null);
		}
	}
	
	public void clearSelectedMalzeme(){
		if(this.getInstance()!=null){
			this.getInstance().setIsemirmalzeme(null);
		}
	}
	
	public void clearSelectedIsemir(){
		if(this.getInstance()!=null){
			this.getInstance().setIsemirByIsemirid(null);
		}
	}
	
	public void setSortMode(String sortMode) {
		this.sortMode = sortMode;
	}

	public String getSortMode() {
		return sortMode;
	}

	public void setSortMode2(String sortMode) {
		this.sortMode2 = sortMode;
	}

	public String getSortMode2() {
		return sortMode2;
	}

	public void setSortMode3(String sortMode) {
		this.sortMode3 = sortMode;
	}

	public String getSortMode3() {
		return sortMode3;
	}
	
	public void setSelectionMode(String selectionMode) {
		this.selectionMode = selectionMode;
	}

	public String getSelectionMode() {
		return selectionMode;
	}

	public void setSelectionMode2(String selectionMode) {
		this.selectionMode2 = selectionMode;
	}

	public String getSelectionMode2() {
		return selectionMode2;
	}
	
	public void setSelectionMode3(String selectionMode) {
		this.selectionMode3 = selectionMode;
	}

	public String getSelectionMode3() {
		return selectionMode3;
	}

	
	public void setTableState(Object tableState) {
		this.tableState = tableState;
	}

	public Object getTableState() {
		return tableState;
	}
	
	public void setTableState2(Object tableState) {
		this.tableState2 = tableState;
	}

	public Object getTableState2() {
		return tableState2;
	}
	
	public void setTableState3(Object tableState) {
		this.tableState3 = tableState;
	}

	public Object getTableState3() {
		return tableState3;
	}
}
